#!/usr/bin/python
# -*- coding: UTF-8 -*-

import thread
import time
import countDownLatch

# 为线程定义一个函数
def print_time( threadName, delay):
   count = 0
   while count < 5:
      time.sleep(delay)
      count += 1
      print "%s: %s" % ( threadName, time.ctime(time.time()) )
   countIndex = latch.count_down()
   print "[%d] %s exit." % ( countIndex, threadName)

threads = []
latch = countDownLatch.CountDownLatch(2)
# 创建两个线程
try:
   thread1 = thread.start_new_thread( print_time, ("Thread-1", 2, ) )
   thread2 = thread.start_new_thread( print_time, ("Thread-2", 4, ) )
   
   
   threads.append(thread1)
   threads.append(thread2)
   
except:
   print "Error: unable to start thread"

# 等待所有线程完成
latch.await()

print "Exiting Main Thread"
   
# while 1:
#   pass